using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Drawing;
using FilteredStatistics.Common;
using FilteredStatistics.Data;

namespace FilteredStatistics.Controller
{
    public class TreeListColumnInfo
    {
        public enum ColumnSortingType
        {
            Unsorted = 0,
            Ascending,
            Descending
        }

        public TreeListColumnInfo(Guid id, IStatisticsProvider statProvider)
        {
            Debug.Assert(id != Guid.Empty);

            m_Id = id;
            m_StatisticProvider = statProvider;
        }

        public TreeListColumnInfo(Guid id, IStatisticsProvider statProvider, int columnWidth,
                                  String columnName, bool isVisible) :
            this(id, statProvider, columnWidth, columnName, isVisible, ColumnSortingType.Unsorted)
        {
        }

        public TreeListColumnInfo(Guid id, IStatisticsProvider statProvider, int columnWidth,
                                  String columnName, bool isVisible, ColumnSortingType sorting) :
            this(id, statProvider)
        {
            HeaderText = columnName;
            IsVisible = isVisible;
            Sorting = sorting;
            ColumnWidth = columnWidth;
        }

        protected void TriggerColumnInfoChangedEvent(string propertyName)
        {
            if (SatisticsColumnInfoChanged != null)
            {
                SatisticsColumnInfoChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public TreeListColumnInfo Clone()
        {
            TreeListColumnInfo copy = new TreeListColumnInfo(Id, Provider, ColumnWidth, HeaderText, IsVisible, Sorting);

            return copy;
        }

        public Guid Id
        {
            get { return m_Id; }
        }

        public IStatisticsProvider Provider
        {
            get { return m_StatisticProvider; }
        }

        public bool IsVisible
        {
            get { return m_IsVisible; }
            set
            {
                if (IsVisible != value)
                {
                    m_IsVisible = value;

                    TriggerColumnInfoChangedEvent("IsVisible");
                }
            }
        }

        public int ColumnWidth
        {
            get { return m_ColumnWidth; }
            set
            {
                if (ColumnWidth != value)
                {
                    m_ColumnWidth = value;

                    TriggerColumnInfoChangedEvent("ColumnWidth");
                }
            }
        }

        public String HeaderText
        {
            get { return m_ColumnName; }
            set
            {
                if (HeaderText != value)
                {
                    m_ColumnName = value;

                    TriggerColumnInfoChangedEvent("ColumnName");
                }
            }
        }

        public ColumnSortingType Sorting
        {
            get { return m_Sorting; }
            set
            {
                if (Sorting != value)
                {
                    m_Sorting = value;

                    TriggerColumnInfoChangedEvent("Sorting");
                }
            }
        }

        public delegate void ColumnInfoChangedEventHandler(TreeListColumnInfo columnInfo, PropertyChangedEventArgs changedProperty);
        public event ColumnInfoChangedEventHandler SatisticsColumnInfoChanged;

        private bool m_IsVisible = true;
        private int m_ColumnWidth = Constants.DefaultColumnWidth;
        private IStatisticsProvider m_StatisticProvider = null;
        private Guid m_Id = Guid.Empty;
        private String m_ColumnName = String.Empty;
        private ColumnSortingType m_Sorting = ColumnSortingType.Unsorted;
    }
}
